package com.smalls.redshoes.ui.fragment;

import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.SurfaceView;
import android.view.View;
import android.widget.AdapterView;

import com.nostra13.universalimageloader.core.download.BaseImageDownloader;
import com.smalls.newvideotwo.base.BaseFragment;
import com.smalls.newvideotwo.mvp.bean.LiveChannel;
import com.smalls.newvideotwo.util.LogUtil;
import com.smalls.newvideotwo.utils.Constant;
import com.smalls.redshoes.R;
import com.smalls.redshoes.ui.widget.LivePlayControlProgramsView.OnLiveProgramItemListener;
import com.smalls.redshoes.ui.widget.LivePlayControlView;
import com.uking.moudle.UKingPlayerIJKImpl;
import com.uking.moudle.UKingPlayerIJKImpl;

import java.util.ArrayList;

public class LivePlayFragment extends BaseFragment implements OnLiveProgramItemListener {
    private static String TAG = Constant.baddcoolio2tag?Constant.TAGPREFIXSTR + LivePlayFragment.class.getSimpleName():LivePlayFragment.class.getSimpleName();
    public static final int DEFAULT_CHANNEL_INDEX = 0;
    public static final int DEFAULT_PROGRAM_INDEX = 0;
    private static final long DELAY_CHECK_ISPLAYING = 3000;
    public static final long DELAY_SET_CAN_SHOW_EC_ERROR = 35000;
    public static final String EXTRA_DATAS = "extra_datas";
    public static final int INVALID_JNI_ERROR_CODE = -1;
    public static final int POP_JIN_ERROR_CODE_INTERVAL = 10000;
    public static final int RETRY_TIME_INTERVAL = 10000;
    private int beginCheckPosition = DEFAULT_PROGRAM_INDEX;
    private boolean canShowEcError = false;
    private boolean isOnpause = false;
    private Handler knJniHander = new Handler() {
        public void handleMessage(Message msg) {
            LogUtil.d(LivePlayFragment.this.TAG, "knJniHander=" + msg.what);
            if (msg.what != BaseImageDownloader.DEFAULT_HTTP_READ_TIMEOUT) {
                LivePlayFragment.this.mJinErrorCode = msg.what;

                LogUtil.d(LivePlayFragment.this.TAG, "canShowEcError=" + LivePlayFragment.this.canShowEcError);
                if (SystemClock.uptimeMillis() - LivePlayFragment.this.mLastSetTime >= 5000
                        && LivePlayFragment.this.mUKingPlayerIJKImpl != null && mUKingPlayerIJKImpl.getMediaPlayer() != null
                        && LivePlayFragment.this.canShowEcError) {
                    LivePlayFragment.this.mLastSetTime = SystemClock.uptimeMillis();
                    LivePlayFragment.this.startCheckPlaying();
                    LogUtil.d(LivePlayFragment.this.TAG, "startCheckPlaying");
                }
            }
        }
    };
    private Runnable mCheckPlayerIsPlayingRunnable = new Runnable() {
        public void run() {
            LogUtil.d(LivePlayFragment.this.TAG, "mCheckPlayerIsPlayingRunnable");
            if (LivePlayFragment.this.mUKingPlayerIJKImpl != null && mUKingPlayerIJKImpl.getMediaPlayer() != null && LivePlayFragment.this.mActivity != null) {
                long endCheckPosition = mUKingPlayerIJKImpl.getMediaPlayer().getCurrentPosition() / 1000;
                LogUtil.d(LivePlayFragment.this.TAG, "beginCheckPosition=" + LivePlayFragment.this.beginCheckPosition + "  endCheckPosition=" + endCheckPosition);
                if ((LivePlayFragment.this.beginCheckPosition < 0 && endCheckPosition < 0) || endCheckPosition <= LivePlayFragment.this.beginCheckPosition) {
                    //if (!(LivePlayFragment.this.mJinErrorCode == LivePlayFragment.INVALID_JNI_ERROR_CODE || LivePlayFragment.this.mJinErrorCode == LivePlayFragment.this.mActivity.getErrorCode())) {
                        //LivePlayFragment.this.mActivity.showErrorToastDialog(LivePlayFragment.this.mJinErrorCode, false);
                    //}
                    LivePlayFragment.this.startCheckPlaying();
                } else if (LivePlayFragment.this.mActivity != null) {
                    //LivePlayFragment.this.mActivity.cancelToastErrorDialog();
                    LivePlayFragment.this.mContentView.removeCallbacks(LivePlayFragment.this.mCheckPlayerIsPlayingRunnable);
                    LivePlayFragment.this.mJinErrorCode = LivePlayFragment.INVALID_JNI_ERROR_CODE;
                }
            }
        }
    };
    private LivePlayControlView mControlView;
    private int mCurrentChannelIndex = DEFAULT_PROGRAM_INDEX;
    private int mCurrentProgramIndex = DEFAULT_PROGRAM_INDEX;
    private ArrayList<LiveChannel> mDatas;
    private int mJinErrorCode = INVALID_JNI_ERROR_CODE;
    private UKingPlayerIJKImpl mUKingPlayerIJKImpl;
    private Handler mUKingPlayerIJKImplHandler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case UKingPlayerIJKImpl.PLAYER_PREPARING /*61696*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_PREPARING");
                    LivePlayFragment.this.mControlView.setState(LivePlayFragment.DEFAULT_PROGRAM_INDEX);
                    return;
                case UKingPlayerIJKImpl.PLAYER_PREPARED /*61697*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_PREPARED");
                    LivePlayFragment.this.mControlView.setState(2);
                    return;
                case UKingPlayerIJKImpl.PLAYER_BUFFERING_START /*61698*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_BUFFERING_START");
                    if (LivePlayFragment.this.mControlView.getState() != 1) {
                        LivePlayFragment.this.mControlView.setState(1);
                    }
                    return;
                case UKingPlayerIJKImpl.PLAYER_BUFFERING_END /*61699*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_BUFFERING_END");
                    LivePlayFragment.this.mControlView.setState(2);
                    return;
                case UKingPlayerIJKImpl.PLAYER_ERROR /*61700*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_ERROR");
                    LivePlayFragment.this.mControlView.setState(LivePlayFragment.INVALID_JNI_ERROR_CODE);
                    if (msg.arg1 != -38) {
                        //LivePlayFragment.this.mActivity.cancelErrorDialog();
                        if (LivePlayFragment.this.mDatas != null
                                && LivePlayFragment.this.mCurrentProgramIndex >= 0
                                && LivePlayFragment.this.mCurrentProgramIndex < LivePlayFragment.this.mDatas.size()) {
                            LivePlayFragment.this.mCurrentChannelIndex = LivePlayFragment.this.mCurrentChannelIndex + 1;
                            if (LivePlayFragment.this.mCurrentChannelIndex > LivePlayFragment.this.mDatas.size() + LivePlayFragment.INVALID_JNI_ERROR_CODE) {
                                LivePlayFragment.this.mCurrentChannelIndex = LivePlayFragment.DEFAULT_PROGRAM_INDEX;
                            }
                        }
                        if (SystemClock.uptimeMillis() - LivePlayFragment.this.mLastReTryTime >= 5000) {
                            LivePlayFragment.this.rePlay();
                            LivePlayFragment.this.mLastReTryTime = SystemClock.uptimeMillis();
                            return;
                        }
                        return;
                    }
                    LivePlayFragment.this.mControlView.setState(-2);
                    return;
                case UKingPlayerIJKImpl.PLAYER_SEEK_END /*61701*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_SEEK_END");
                    return;
                case UKingPlayerIJKImpl.PLAYER_PROGRESS_INFO /*61702*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_PROGRESS_INFO");
                    LivePlayFragment.this.mControlView.setState(2);
                    return;
                case UKingPlayerIJKImpl.PLAYER_COMPLETE /*61703*/:
                    LogUtil.d(LivePlayFragment.this.TAG, "PLAYER_COMPLETE  code==" + msg.what);
                    return;
                default:
                    return;
            }
        }
    };
    private long mLastReTryTime = 0;
    private long mLastSetTime = 0;
    private SurfaceView mSurfaceView;
    private OnLiveProgramItemListener onLiveProgramItemListener;
    private Runnable setCanShowEcErrorRunnable = new Runnable() {
        public void run() {
            LivePlayFragment.this.canShowEcError = true;
            LogUtil.d(LivePlayFragment.this.TAG, "setCanShowEcErrorRunnable----code=" + LivePlayFragment.this.mJinErrorCode + "---knJniHander=" + LivePlayFragment.this.knJniHander);
            if (LivePlayFragment.this.mJinErrorCode != LivePlayFragment.INVALID_JNI_ERROR_CODE && LivePlayFragment.this.knJniHander != null) {
                LogUtil.d(LivePlayFragment.this.TAG, "sendEmptyMessage(mJinErrorCode)=" + LivePlayFragment.this.mJinErrorCode);
                LivePlayFragment.this.knJniHander.sendEmptyMessage(LivePlayFragment.this.mJinErrorCode);
            }
        }
    };

    public void onAttach(Activity context) {
        super.onAttach(context);
        Bundle bundle = getArguments();
        if (bundle != null) {
            this.mDatas = (ArrayList) bundle.getSerializable(EXTRA_DATAS);
        }
    }

    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.fragment_live_play);
        this.mSurfaceView = (SurfaceView) getViewById(R.id.fragment_live_play_sv);
        this.mControlView = (LivePlayControlView) getViewById(R.id.fragment_live_play_control_view);
    }

    public void requestFocus() {
        if (this.mControlView != null) {
            this.mControlView.setFocusable(true);
            this.mControlView.requestFocus();
        }
    }

    public void clearFocus() {
        if (this.mControlView != null) {
            this.mControlView.setFocusable(false);
            this.mControlView.clearFocus();
        }
    }

    public void updateControlViewVisibility() {
        if (this.mControlView != null) {
            this.mControlView.updateVisibity();
        }
    }

    public int getControlViewVisibility() {
        return this.mControlView == null ? 8 : this.mControlView.isVisibility();
    }

    public void setControlViewGone() {
        if (this.mControlView != null) {
            this.mControlView.setGone();
        }
    }

    public boolean setControlViewVisible(int keyCode) {
        if (this.mControlView != null) {
            return this.mControlView.setVisible(keyCode);
        }
        return false;
    }

    public void changeProgram(int position) {
        if (this.mControlView != null) {
            this.mControlView.changeProgram(position);
        }
    }

    private void initPlayer() {
        this.mUKingPlayerIJKImpl = new UKingPlayerIJKImpl(this.mActivity, this.mSurfaceView, this.mUKingPlayerIJKImplHandler, null, this.knJniHander, true);
    }

    public void setTime() {
        if (this.mControlView != null) {
            this.mControlView.setTime();
        }
    }

    protected void setListener() {
        this.mControlView.setOnLiveProgramItemListener(this);
    }

    protected void processLogic(Bundle savedInstanceState) {
        this.mControlView.setmDatas(this.mDatas);
        initPlayer();
    }

    private void playUrl() {
        if (this.mDatas != null) {
            LiveChannel liveChannelModels = this.mDatas.get(this.mCurrentProgramIndex);
            String mediaCode = liveChannelModels.getMedia_code();
            if (mediaCode!=null&&!TextUtils.isEmpty(mediaCode)) {
                mUKingPlayerIJKImpl.stop();
                this.mUKingPlayerIJKImpl.playUrl(mediaCode);
                this.mControlView.removeCallbacks(this.setCanShowEcErrorRunnable);
                this.canShowEcError = false;
                this.mControlView.postDelayed(this.setCanShowEcErrorRunnable, DELAY_SET_CAN_SHOW_EC_ERROR);
                this.mJinErrorCode = INVALID_JNI_ERROR_CODE;
                this.mControlView.setState(DEFAULT_PROGRAM_INDEX);
                LogUtil.d(this.TAG, "mediaCode=" + mediaCode);
            }
        }
    }

    private void rePlay() {
        this.mUKingPlayerIJKImpl.stop();
        playUrl();
    }

    public void setOnLiveProgramItemListener(OnLiveProgramItemListener onLiveProgramItemListener) {
        this.onLiveProgramItemListener = onLiveProgramItemListener;
    }

    public void onLiveProgramItemClicked(AdapterView<?> parent, View view, int position, long id) {
        LogUtil.d(this.TAG, "onLiveProgramItemClicked=" + position);
        this.mCurrentProgramIndex = position;
        playUrl();
        if (this.onLiveProgramItemListener != null) {
            this.onLiveProgramItemListener.onLiveProgramItemClicked(parent, view, position, id);
        }
    }

    public void setDatas(ArrayList<LiveChannel> mDatas) {
        this.mDatas = mDatas;
        if (this.mControlView != null) {
            this.mControlView.setmDatas(this.mDatas);
        }
        LogUtil.d(this.TAG, "setDatas");
    }

    public void onResume() {
        super.onResume();
        if (this.mUKingPlayerIJKImpl != null && this.isOnpause) {
            playUrl();
        }
        this.isOnpause = false;
    }

    public void onPause() {
        super.onPause();
        this.isOnpause = true;
    }

    public void clearResouse() {
        if (this.mControlView != null) {
            this.mControlView.destory();
        }
        if (this.mUKingPlayerIJKImpl != null) {
            this.mUKingPlayerIJKImpl.stop();
            this.mUKingPlayerIJKImpl.close();
        }
    }

    public void onDestroy() {
        clearResouse();
        super.onDestroy();
    }

    private void startCheckPlaying() {
        if (this.mUKingPlayerIJKImpl != null && this.mUKingPlayerIJKImpl.getMediaPlayer() != null) {
            this.mContentView.removeCallbacks(this.mCheckPlayerIsPlayingRunnable);
            this.beginCheckPosition = (int)mUKingPlayerIJKImpl.getMediaPlayer().getCurrentPosition() / 1000;
            this.mContentView.postDelayed(this.mCheckPlayerIsPlayingRunnable, DELAY_CHECK_ISPLAYING);
        }
    }

    public void onErrorToastDialogCancel() {
        if (this.mContentView != null && this.mCheckPlayerIsPlayingRunnable != null) {
            this.mContentView.removeCallbacks(this.mCheckPlayerIsPlayingRunnable);
            this.mLastSetTime = SystemClock.uptimeMillis();
        }
    }
}
